from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn import metrics, svm
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
import tensorflow as tf
from tensorflow import keras
import pandas as pd

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# path = "rawdata2.csv"
# 在服务上跑的数据是20201226日生成的data
path = "D:/learn/school/code/myfinalpaper/data/rawdata.csv"
df = pd.read_csv(path)
x = df['data']
y = df['label']
y = y.to_numpy()

def do_metrics(y_test_truth, y_test_pred):
    print("metrics.accuracy_score:")
    print(metrics.accuracy_score(y_test_truth, y_test_pred))
    print("metrics.confusion_matrix:")
    print(metrics.confusion_matrix(y_test_truth, y_test_pred))
    print("metrics.precision_score:")
    print(metrics.precision_score(y_test_truth, y_test_pred))
    print("metrics.recall_score:")
    print(metrics.recall_score(y_test_truth, y_test_pred))
    print("metrics.f1_score:")
    print(metrics.f1_score(y_test_truth, y_test_pred))

def model_prediction(model, name, x_test, y_test):
    y_predict_list = model.predict(x_test)
    y_predict = []
    for i in y_predict_list:
        if i[0] > 0.5:
            y_predict.append(0)
        else:
            y_predict.append(1)
    do_metrics(y_test, y_predict)
    dataframe = pd.DataFrame({'y_test_truth': y_test,
                              'y_predict_score': y_predict_list, #这个才是最终的内容，不要再修改其中的值了
                              'y_predict_label': y_predict})
    dataframe.to_csv(name + '.csv', sep=',', index=False)


def do_mlp(x,y):
    #mlp
    clf = MLPClassifier(solver='lbfgs',
                        alpha=1e-5,
                        hidden_layer_sizes=(5, 2),
                        random_state=1)

    # 划分测试集和训练集
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=0)
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    print("*****************************************")
    print("用mlp模型预测")
    do_metrics(y_test,y_pred)
    print("预测结束")
    print("*****************************************")
    dataframe = pd.DataFrame({'y_test_truth': y_test,
                              'y_predict_score': clf.predict_proba(x_test)[:, 1],
                              'y_predict_label': y_pred})
    dataframe.to_csv("mlp" + '.csv', sep=',', index=False)

def tf_idf_bigru(x, y):
    cv = CountVectorizer(max_features=10000)
    x = cv.fit_transform(x).toarray()
    # 转换为tfidf
    x = TfidfTransformer().fit_transform(x).toarray()
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
    x_train_padded_seqs = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=400, padding='post')
    x_test_padded_seqs = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=400, padding='post')
    model = keras.Sequential([
        keras.layers.Embedding(20480, 128),
        keras.layers.Bidirectional(tf.keras.layers.GRU(128)),
        keras.layers.Dropout(0.2),
        keras.layers.Dense(1, activation="softmax")
    ])
    model.compile(loss = tf.keras.losses.BinaryCrossentropy(from_logits=True),
                  optimizer=tf.keras.optimizers.Adam(0.001),
                  metrics=['accuracy'])
    model.fit(x_train_padded_seqs,
                        y_train,
                        epochs=5,
                        validation_data=(x_test_padded_seqs, y_test),
                        batch_size = 32,
                        shuffle = True
                        )
    model_prediction(model, "tf_idf_bigru", x_test, y_test)


tf_idf_bigru(x, y)


